package com.knife.blog.logic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.knife.web.tools.ActiveUser;

/**
 * 
 */
public class ActiveUserService implements java.lang.Runnable {
	private static ActiveUserService singleton = new ActiveUserService();

	public static ActiveUserService getInstance() {
		return singleton;
	}

	private final java.util.Map admins = new java.util.HashMap();

	private final java.util.Map managers = new java.util.HashMap();

	private final java.util.Map users = new java.util.HashMap();

	private final java.util.Map guests = new java.util.HashMap();

	private long timeOut = 1000 * 60 * 10;// 定义最大超时时间

	private long refreshTime = 1000 * 60 * 2;// 定义刷新时间

	public void run() {
		while (true) {
			try {
				System.out.println("当前在线人数:" + getAllUserNum());
				Thread.sleep(refreshTime);
				refreshOnline(admins.values());
				refreshOnline(managers.values());
				refreshOnline(users.values());
				refreshOnline(guests.values());
			} catch (Exception e) {
				System.out.println("在线人数控制线程出现错误：" + e);
			}
		}
	}

	private void refreshOnline(java.util.Collection c) {
		java.util.Collection delC = new java.util.ArrayList();
		Iterator it = c.iterator();
		long now = System.currentTimeMillis();
		while (it.hasNext()) {
			ActiveUser user = (ActiveUser) it.next();
			/*if (now - user.getLastVisitTime().getTime() > timeOut)
				delC.add(user);*/
		}
		c.removeAll(delC);
	}

	public boolean isOnline(String username) {
		return admins.containsKey(username) || managers.containsKey(username)
				|| users.containsKey(username) || guests.containsKey(username);

	}

	public ActiveUser getUser(String username) {
		ActiveUser user = null;
		user = (ActiveUser) admins.get(username);
		if (user == null)
			user = (ActiveUser) managers.get(username);
		if (user == null)
			user = (ActiveUser) users.get(username);
		if (user == null)
			user = (ActiveUser) guests.get(username);
		return user;
	}

	/*public void join(ActiveUser user) {
		if (!isOnline(user.getUserName())) {
			BBSUserRole userRole = BBSUserRole.getUserRole(null, user);
			if (userRole.isAdministrator())
				admins.put(user.getUserName(), user);
			else if (userRole.isValidateUser())
				users.put(user.getUserName(), user);
			else if (userRole.isBssManager())
				managers.put(user.getUserName(), user);
			else
				guests.put(user.getUserName(), user);
		}
	}*/

	public void remove(ActiveUser user) {
		if (user == null)
			return;
		this.admins.remove(user.getUserName());
		this.managers.remove(user.getUserName());
		this.users.remove(user.getUserName());
		this.guests.remove(user.getUserName());
	}

	public List getAdmins() {
		return new ArrayList(admins.values());
	}

	public List getManagers() {
		return new ArrayList(managers.values());
	}

	public List getUsers() {
		return new ArrayList(users.values());
	}

	public List getGuests() {
		return new ArrayList(guests.values());
	}

	public List getAllUser() {
		java.util.List list = new ArrayList();
		list.addAll(admins.values());
		list.addAll(managers.values());
		list.addAll(users.values());
		list.addAll(guests.values());
		return list;
	}

	public Integer getAllUserNum() {
		return new Integer(admins.size() + managers.size() + users.size()
				+ guests.size());
	}

	public Integer getAdminNum() {
		return new Integer(admins.size());
	}

	public Integer getManagerNum() {
		return new Integer(managers.size());
	}

	public Number getUsersNum() {
		return new Integer(users.size());
	}

	public Number getGuestNum() {
		return new Integer(guests.size());
	}

	public long getRefreshTime() {
		return refreshTime;
	}

	public void setRefreshTime(long refreshTime) {
		this.refreshTime = refreshTime;
	}

	public long getTimeOut() {
		return timeOut;
	}

	public void setTimeOut(long timeOut) {
		this.timeOut = timeOut;
	}
}